package com.ws.universal.tools.takephoto.crop;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import androidx.fragment.app.Fragment;

import com.ws.universal.tools.R;
import com.ws.universal.tools.utils.ToastUtils;


/**
 * @author:ws
 * @date: 2020/9/1 17:00
 * @desc
 */
public class Crop {
    public static final int REQUEST_CROP = 6709;
    public static final int REQUEST_PICK = 9162;
    public static final int RESULT_ERROR = 404;

    private Intent cropIntent = new Intent();

    private Crop(Uri source, Uri destination) {
        this.cropIntent.setData(source);
        this.cropIntent.putExtra("output", destination);
    }

    public static Crop of(Uri source, Uri destination) {
        return new Crop(source, destination);
    }

    public static Uri getOutput(Intent result) {
        return (Uri) result.getParcelableExtra("output");
    }

    public static Throwable getError(Intent result) {
        return (Throwable) result.getSerializableExtra("error");
    }

    public static void pickImage(Activity activity) {
        pickImage(activity, 9162);
    }

    public static void pickImage(Context context, Fragment fragment) {
        pickImage(context, (Fragment) fragment, 9162);
    }

    public static void pickImage(Activity activity, int requestCode) {
        try {
            activity.startActivityForResult(getImagePicker(), requestCode);
        } catch (ActivityNotFoundException var3) {
            showImagePickerError(activity);
        }

    }

    @TargetApi(11)
    public static void pickImage(Context context, Fragment fragment, int requestCode) {
        try {
            fragment.startActivityForResult(getImagePicker(), requestCode);
        } catch (ActivityNotFoundException var4) {
            showImagePickerError(context);
        }

    }

    private static Intent getImagePicker() {
        return (new Intent("android.intent.action.GET_CONTENT")).setType("image/*");
    }

    @SuppressLint("WrongConstant")
    private static void showImagePickerError(Context context) {
        ToastUtils.showShort(context.getResources().getString(R.string.crop__pick_error));
    }

    //
    public Crop withAspect(int x, int y) {
        this.cropIntent.putExtra("aspect_x", x);
        this.cropIntent.putExtra("aspect_y", y);
        return this;
    }

    public Crop setCropTunch(boolean isCropTunch) {
        this.cropIntent.putExtra("isCropTunch", isCropTunch);
        return this;
    }

    /**
     * 设置是否将结果保存为 PNG。
     *
     * @param asPng
     * @return
     */
    public Crop asPng(boolean asPng) {
        this.cropIntent.putExtra("as_png", asPng);
        return this;
    }

    public void start(Activity activity) {
        this.start(activity, 6709);
    }

    public void start(Activity activity, int requestCode) {
        activity.startActivityForResult(this.getIntent(activity), requestCode);
    }

    public void start(Context context, Fragment fragment) {
        this.start(context, (Fragment) fragment, 6709);
    }

    @TargetApi(11)
    public void start(Context context, Fragment fragment, int requestCode) {
        fragment.startActivityForResult(this.getIntent(context), requestCode);
    }

    public Intent getIntent(Context context) {
        this.cropIntent.setClass(context, CropImageActivity.class);
        return this.cropIntent;
    }


}
